//
//  LXStatisticsScreenView.m
//  ZuLinProject
//
//  Created by bancheng on 2022/9/5.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXStatisticsScreenView.h"
#import "LXScreenTableViewCell.h"
#import "LXStatisticsModel.h"
@interface LXStatisticsScreenView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic) UITableView *tableView;
@end

@implementation LXStatisticsScreenView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [[UIColor colorWithHexString:@"#4E4E4E"] colorWithAlphaComponent:0.5];
    }
    return self;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if (self.clickCellBlock) {
        self.clickCellBlock(nil);
    }
}

//- (void)layoutSubviews{
//    [super layoutSubviews];
//
//
//    [self addSubview:self.tableView];
//}
    
    
- (void)setData:(NSArray *)data{
    _data = data;
    
    
    [self addSubview:self.tableView];
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(data.count * 44);
    }];
    
    [self.tableView reloadData];
}
    
    
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LXScreenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXScreenTableViewCell class])];
    LXStatisticsModel *model = self.data[indexPath.row];
    cell.textLab.text = model.name;
    if (model.isSel) {
        cell.selImageV.hidden = NO;
        cell.textLab.textColor = ColorWith3C6CFC;
    }
    else {
        cell.selImageV.hidden = YES;
        cell.textLab.textColor = ColorWith333333;
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.data setValue:@(NO) forKey:@"isSel"];
    LXStatisticsModel *model = self.data[indexPath.row];
    model.isSel = YES;    
    
    if (self.clickCellBlock) {
        self.clickCellBlock(model);
    }
    
    

}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [self addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.height.mas_equalTo(0);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.rowHeight = 44;
        _tableView.contentInset = UIEdgeInsetsMake(0,0,kComHeight+10,0);
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXScreenTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXScreenTableViewCell class])];
    }
    return _tableView;
}
@end
